package handlers

import (
	"code/config"
	"code/utils"
	"fmt"
	"strings"
)

// UpdateHandler 处理根据给定配置生成更新代码的逻辑
func UpdateHandler(cfg config.Config) config.CodeResult {
	var codes config.CodeResult
	blue.Printf("开始生成 %s 的代码\n", cfg.Action)

	codeController := controllerBuildUpdate(cfg)
	codeControllerContent := fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, codeController[0], cfg.EndTag)

	codes.ControllerCode = codeControllerContent
	codes.ServiceCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceBuildUpdate(cfg)[0], cfg.EndTag)
	codes.ServiceImplCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceImplBuildUpdate(cfg)[0], cfg.EndTag)
	codes.MapperCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, mapperBuildUpdate(cfg)[0], cfg.EndTag)
	codes.MapperXMLCode = fmt.Sprintf("<!--%s-->\n%s\n<!--%s-->\r\n\n", cfg.StartTag, mapperXMLBuildUpdate(cfg)[0], cfg.EndTag)

	green.Printf("生成完毕\n%s\n", cfg.Name)
	return codes
}

// 控制器构建函数 - 更新
func controllerBuildUpdate(config config.Config) []string {
	controllerTemplate := `
// 控制器
@{ApiType}Mapping("{Api}")
public AjaxResult update{tag}(@RequestBody {Name} {nameVariable}) {
    return toAjax({service}.update{Name}{tag}({nameVariable}));
}
`
	controllerCode := strings.ReplaceAll(controllerTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{tag}", config.Tag)
	controllerCode = strings.ReplaceAll(controllerCode, "{nameVariable}", utils.ToCamelCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{service}", utils.ToCamelCase(config.Table)+"Service")
	controllerCode = strings.ReplaceAll(controllerCode, "{ApiType}", utils.ToPascalCase(config.ApiType))
	controllerCode = strings.ReplaceAll(controllerCode, "{Api}", config.Api)

	return []string{controllerCode}
}

// 服务构建函数 - 更新
func serviceBuildUpdate(config config.Config) []string {
	serviceTemplate := `
/**
 * 更新 {Name} 服务
 * 
 * @param {Name} {nameVariable}
 * @return 影响行数
 */
public int update{Name}{tag}({Name} {nameVariable});
`
	serviceCode := strings.ReplaceAll(serviceTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceCode = strings.ReplaceAll(serviceCode, "{tag}", config.Tag)
	serviceCode = strings.ReplaceAll(serviceCode, "{nameVariable}", utils.ToCamelCase(config.Table))

	return []string{serviceCode}
}

// 服务实现构建函数 - 更新
func serviceImplBuildUpdate(config config.Config) []string {
	serviceImplTemplate := `
// {Name}服务实现	
@Override
public int update{Name}{tag}({Name} {nameVariable}) {
    return {mapper}.update{Name}{tag}({nameVariable});
}
`
	serviceImplCode := strings.ReplaceAll(serviceImplTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{tag}", config.Tag)
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{nameVariable}", utils.ToCamelCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{mapper}", utils.ToCamelCase(config.Table)+"Mapper")

	return []string{serviceImplCode}
}

// Mapper构建函数 - 更新
func mapperBuildUpdate(config config.Config) []string {
	mapperTemplate := `
// Mapper
/**
 * 更新 {Name}
 * 
 * @param {Name} {nameVariable}
 * @return 影响行数
 */
public int update{Name}{tag}({Name} {nameVariable});
`
	mapperCode := strings.ReplaceAll(mapperTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperCode = strings.ReplaceAll(mapperCode, "{tag}", config.Tag)
	mapperCode = strings.ReplaceAll(mapperCode, "{nameVariable}", utils.ToCamelCase(config.Table))

	return []string{mapperCode}
}

// Mapper XML构建函数 - 更新
func mapperXMLBuildUpdate(config config.Config) []string {
	mapperXMLTemplate := `
<update id="update{Name}{tag}" parameterType="{Name}">
    UPDATE {table}
    <set>
        <if test="sort != null "> sort = #{sort}, </if>
        <if test="isEnable != null "> is_enable = #{isEnable}, </if>
        <if test="title != null and title != ''"> title = #{title}, </if>
        <if test="displayTime != null "> display_time = #{displayTime}, </if>
        <if test="cover != null and cover != ''"> cover = #{cover}, </if>
        <if test="content != null and content != ''"> content = #{content}, </if>
        <if test="info != null and info != ''"> info = #{info}, </if>
        <!-- 新增字段 -->
        <if test="copyfrom != null and copyfrom != ''"> copyfrom = #{copyfrom}, </if>
        <if test="keyword != null and keyword != ''"> keyword = #{keyword}, </if>
        <if test="focus != null "> focus = #{focus}, </if>
        <if test="sug != null "> sug = #{sug}, </if>
    </set>
    WHERE id = #{id}
</update>
`
	mapperXMLCode := strings.ReplaceAll(mapperXMLTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{tag}", config.Tag)
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{table}", config.Table)

	return []string{mapperXMLCode}
}
